﻿
function fireParentForm(LevelLocationType, WebpartId, PermissionCode) { }
// Export data

function showhide(a,id)   
                 {   
                     var e = document.getElementById(id);   
                     if (e.style.display == 'none')    
                     {   
                         e.style.display = 'block';   
                         a.innerHTML = 'Ẩn tìm kiếm nâng cao';   
                     }   
                     else    
                     {   
                         e.style.display = 'none';   
                         a.innerHTML = 'Tìm kiếm nâng cao';   
                     }   
                 }    
function ClientSideExportDataCase() {

    if (CheckValidData() == false) return;
    var LocationId = $("#_locationsel").val();


    if (document.getElementById("_locationLevelTypeSelect") == null) {
        alert("Bạn chưa chọn cấp địa phương");
        return;
    }
    else {
        if (document.getElementById("_locationLevelTypeSelect").selectedIndex == 0) {
            alert("Bạn chưa chọn cấp địa phương");
            return;
        }
    }


    rValue = null;
    var toidanh = null;
    var result = document.getElementById("ContentSearchResult");

    var fromdate = document.getElementById("drpDaytxt_fdate").options[document.getElementById("drpDaytxt_fdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_fdate").options[document.getElementById("drpMonthtxt_fdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_fdate").options[document.getElementById("drpYeartxt_fdate").selectedIndex].value;
    var todate = document.getElementById("drpDaytxt_tdate").options[document.getElementById("drpDaytxt_tdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_tdate").options[document.getElementById("drpMonthtxt_tdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_tdate").options[document.getElementById("drpYeartxt_tdate").selectedIndex].value;
//    if (fromdate.length > 0) {
//        var arryear = fromdate.split('/');
//        fromdate = arryear[1] + '/' + arryear[0] + '/' + arryear[2];
//    }
//    if (todate.length > 0) {
//        var arryear = todate.split('/');
//        todate = arryear[1] + '/' + arryear[0] + '/' + arryear[2];
//    }

    var selectedRow = document.getElementById("DrpSelectRow").options[document.getElementById("DrpSelectRow").selectedIndex].value;
    var selectedCol = document.getElementById("DrpSelectCol").options[document.getElementById("DrpSelectCol").selectedIndex].value;

    

    rValue = ClientDoSearch();
    result.style.display = "block";
    document.getElementById("divProcessingSearchResult").innerHTML = Portal.SODC.IDIS.CaseInformation.GetProcessingText().value;

    Ret = Portal.SODC.IDIS.CaseInformation.ServerExportData(LocationId, fromdate, todate, selectedRow, selectedCol).value;

//    Ret = Portal.SODC.IDIS.CaseInformation.ServerExportData(LocationId).value;
    document.getElementById("divProcessingSearchResult").innerHTML = "Hệ thống đang xử lý...";
    if (Ret.indexOf("ERROR") != -1) {
        alert(Ret);
        document.getElementById("divProcessingSearchResult").innerHTML = "";
        return;
    } else {
        document.getElementById("divProcessingSearchResult").innerHTML = "";
        window.open(Ret, "_blank");
    }
}

function GetItemCaseSubject() {
    var allVals = [];
    $('#sl_toidanh :checked').each(function () {
        allVals.push($(this).val());
    });
    var text = allVals;
    return text;
}

function selectLevelTypeForListCase() {
    var ltype = $("#selectlevel").val();
    $("#SelectLocationZone").html(Portal.SODC.IDIS.CaseInformation.ServerSideRenderLocationSelect(ltype).value);
}

//For search Adv
function ExecuteSearch() {
    rValue = ClientDoSearch();
}

function ExecuteSearchCase() {
    rValue = ClientDoSearch();
}

function ClientDoSimpleSearchCase() {

    if (CheckValidData() == false) return;

    var LocationId = $("#_locationsel").val();
    rValue = null;
    var toidanh = null;
    var result = document.getElementById("ContentSearchResult");

    var fromdate = document.getElementById("drpDaytxt_fdate").options[document.getElementById("drpDaytxt_fdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_fdate").options[document.getElementById("drpMonthtxt_fdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_fdate").options[document.getElementById("drpYeartxt_fdate").selectedIndex].value;
    var todate = document.getElementById("drpDaytxt_tdate").options[document.getElementById("drpDaytxt_tdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_tdate").options[document.getElementById("drpMonthtxt_tdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_tdate").options[document.getElementById("drpYeartxt_tdate").selectedIndex].value;
//    var fromdate = document.getElementById("txt_fdate").value;
//    var todate = document.getElementById("txt_tdate").value;
//    if (fromdate.length > 0) 
//    {
//        var arryear = fromdate.split('/');
//        fromdate = arryear[1] + '/' + arryear[0] + '/' + arryear[2];
//    }
//    if (todate.length > 0) {
//        var arryear = todate.split('/');
//        todate = arryear[1] + '/' + arryear[0] + '/' + arryear[2];
//    }

    var selectedRow = document.getElementById("DrpSelectRow").options[document.getElementById("DrpSelectRow").selectedIndex].value;
    var selectedCol = document.getElementById("DrpSelectCol").options[document.getElementById("DrpSelectCol").selectedIndex].value;

    rValue = ClientDoSearch();
    result.style.display = "block";
    document.getElementById("divProcessingSearchResult").innerHTML = Portal.SODC.IDIS.CaseInformation.GetProcessingText().value;

    //Ret = Portal.SODC.IDIS.CaseInformation.GetResult(LocationId,fromdate, todate, rValue, toidanh, selectedRow, selectedCol).value;
    Ret = Portal.SODC.IDIS.CaseInformation.GetResult(LocationId, fromdate, todate, selectedRow, selectedCol).value;
    document.getElementById("divProcessingSearchResult").innerHTML = "";
    if (Ret.indexOf("ERROR") != -1) {
        alert(Ret.replace("ERROR:", "").trim());
        result.innerHTML = "";
        return;
    }
    result.innerHTML = Ret;
}

function getData() {
    myData = document.getElementById("select_age").options[document.getElementById("select_age").selectedIndex].value;
    return myData;
}

function ResetAllFieldCase() {
    var textBoxes = $("input[type='text']");
    for (var i = 0; i < textBoxes.length; i++) {
        textBoxes[i].value = "0";
    }
    document.getElementById("txt_mavuan").value = '';
    document.getElementById("txt_ngayvuan").value = '';
    document.getElementById("txt_tieude").value = '';
    document.getElementById("txt_ngayxetxu").value = '';
}

// for AdvSearch
function ShowAdvSearch() {
//    em1 = document.getElementById("divSimpleSearch");
    em = document.getElementById("advSearch");
    if (em.style.display == "block") {
        em.style.display = "none";
//        em1.style.display = "block";
    }
    else {
        em.style.display = "block";
//        em1.style.display = "none";
    }
}

function ShowAdvSearchCase() {
//    em1 = document.getElementById("divSimpleSearchCase");
    em = document.getElementById("advSearchCase");
    if (em.style.display == "block") {
        em.style.display = "none";
//        em1.style.display = "block";
    }
    else {
        em.style.display = "block";
//        em1.style.display = "none";
    }
}

function CloseFormInputCase() {
    var em;
    em = document.getElementById("insnewcasepanel");
    em.style.display = 'none';
}
function CheckValidData() {
    fromyear = document.getElementById("drpDaytxt_fdate").options[document.getElementById("drpDaytxt_fdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_fdate").options[document.getElementById("drpMonthtxt_fdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_fdate").options[document.getElementById("drpYeartxt_fdate").selectedIndex].value;
    toyear = document.getElementById("drpDaytxt_tdate").options[document.getElementById("drpDaytxt_tdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_tdate").options[document.getElementById("drpMonthtxt_tdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_tdate").options[document.getElementById("drpYeartxt_tdate").selectedIndex].value;
//    fromyear = document.getElementById("txt_fdate").value;
//    toyear = document.getElementById("txt_tdate").value;
    if (fromyear.length == 0 || toyear == 0) {
        alert("Bạn phải chọn thời gian để tạo thống kê!")
        return false;
    }

    var vallocation = $("#selectlevel").val();

    var sublocation = $("#_locationsel").val();
    if (vallocation == 0 || sublocation == 0) {
        alert("Bạn phải chọn cấp - địa phương để xuất dữ liệu !")
        return false;
    }

    ColIndex = document.getElementById("DrpSelectCol").selectedIndex;
    ColVal = document.getElementById("DrpSelectCol").options[ColIndex].value;

    RowIndex = document.getElementById("DrpSelectRow").selectedIndex;
    RowVal = document.getElementById("DrpSelectRow").options[RowIndex].value;
    if (RowVal == ColVal) {
        alert('Giá trị Cột và Hàng phải khác nhau.');
        return false;
    }

    if (ColVal == 'Year') {
        var arryear = fromyear.split('/');
        var year = arryear[2];

        var arrtoyear = toyear.split('/');
        var tyear = arrtoyear[2];
        if (year == tyear) {
            alert('Thống kê năm không thực hiện với khoảng thời gian trong một năm.');
            return false;
        }
    }

    if (ColVal == 'Month') {
        var arryear = fromyear.split('/');
        var year = arryear[2];

        var arrtoyear = toyear.split('/');
        var tyear = arrtoyear[2];
        if (year != tyear) {
            alert('Thống kê tháng chỉ thực hiện với khoảng thời gian trong một năm.');
            return false;
        }
    }
    return true;
}

function ClientDoSearch() 
{

    if (CheckValidData() == false) return;

    var LocationId = '';
    if ($("#_locationsel") != null) {
        document.getElementById("divProcessingSearchResult").innerHTML = "Chọn dữ liệu của đơn vị thống kê!";
    }
        LocationId = $("#_locationsel").val();

//    var rValue = null;
    
    var result = document.getElementById("ContentSearchResult");


    var fromdate = document.getElementById("drpDaytxt_fdate").options[document.getElementById("drpDaytxt_fdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_fdate").options[document.getElementById("drpMonthtxt_fdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_fdate").options[document.getElementById("drpYeartxt_fdate").selectedIndex].value;
    var todate = document.getElementById("drpDaytxt_tdate").options[document.getElementById("drpDaytxt_tdate").selectedIndex].value + '/' + document.getElementById("drpMonthtxt_tdate").options[document.getElementById("drpMonthtxt_tdate").selectedIndex].value + '/' + document.getElementById("drpYeartxt_tdate").options[document.getElementById("drpYeartxt_tdate").selectedIndex].value;
//    if (fromdate.length > 0) 
//    {
//        var arryear = fromdate.split('/');
//        fromdate = arryear[1] + '/' + arryear[0] + '/' + arryear[2];
//    }
//    if (todate.length > 0) {
//        var arryear = todate.split('/');
//        todate = arryear[1] + '/' + arryear[0] + '/' + arryear[2];
//    }
//    if (document.getElementById("locationLevelTypeSelect") != null) {

//        if (document.getElementById("locationLevelTypeSelect").selectedIndex == 0) {

//            alert("Bạn chưa chọn cấp địa phương");
//            return;
//        }
//    }
    if (document.getElementById("_locationLevelTypeSelect") == null) {


        alert("Bạn chưa chọn cấp địa phương");
        return;


    }
    else {
        if (document.getElementById("_locationLevelTypeSelect").selectedIndex == 0) {
            alert("Bạn chưa chọn cấp địa phương");
            return;
        }
    }



    

    document.getElementById("divProcessingSearchResult").innerHTML = Portal.SODC.IDIS.CaseInformation.GetProcessingText().value;
//    CaseSubject = $("#txt_tieude").val();
    var selectedRow = document.getElementById("DrpSelectRow").options[document.getElementById("DrpSelectRow").selectedIndex].value;
    var selectedCol = document.getElementById("DrpSelectCol").options[document.getElementById("DrpSelectCol").selectedIndex].value;

//    rValue = ClientDoSearch();

    RetValue = Portal.SODC.IDIS.CaseInformation.GetResult(LocationId, fromdate, todate, selectedRow, selectedCol).value;

    if (RetValue.indexOf("ERROR") != -1) {
        alert(RetValue.replace("ERROR:", "").trim());
        result.innerHTML = "";
        return;
    }
    if (RetValue.indexOf("Nodata") != -1)
    {
        result.style.display = "none";
        document.getElementById("divProcessingSearchResult").style.display = "block";
        document.getElementById("divProcessingSearchResult").innerHTML = "Không có dữ liệu!";
    } 
    else 
    {
        result.innerHTML = RetValue;
        document.getElementById("divProcessingSearchResult").style.display = "none";
    }
}
